package com.farm.service.impl;

import com.farm.constant.MessageConstant;
import com.farm.constant.StatusConstant;
import com.farm.dto.GiftDTO;
import com.farm.dto.GiftPageQueryDTO;
import com.farm.entity.Category;
import com.farm.entity.Gift;
import com.farm.entity.GiftGoods;
import com.farm.entity.Goods;
import com.farm.exception.DeletionNotAllowedException;
import com.farm.mapper.CategoryMapper;
import com.farm.mapper.GiftGoodsMapper;
import com.farm.mapper.GiftMapper;
import com.farm.mapper.GoodsMapper;
import com.farm.result.PageResult;
import com.farm.service.GiftService;
import com.farm.vo.GiftVO;
import com.farm.vo.GoodsItemVO;
import com.farm.vo.GoodsVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @FileName GiftServiceImpl
 * @Description
 * @Author Sword
 * @date 2025-02-25
 **/

@Service
@Slf4j
public class GiftServiceImpl implements GiftService {

    @Autowired
    private GiftMapper giftMapper;

    @Autowired
    private GiftGoodsMapper giftGoodsMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 新增礼盒
     *
     * @param giftDTO
     */
    @Transactional
    @Override
    public void save(GiftDTO giftDTO) {
        Gift gift = new Gift();
        BeanUtils.copyProperties(giftDTO, gift);

        giftMapper.insert(gift);

        Long id = gift.getId();

        List<GiftGoods> giftGoods = giftDTO.getGiftGoods();

        giftGoods.forEach(gg -> {
            gg.setGiftId(id);
            Goods goods = goodsMapper.getById(gg.getGoodsId());
            gg.setName(goods.getName());
        });

        giftGoodsMapper.saveBatch(giftGoods);
    }

    /**
     * 分页查询
     *
     * @param giftPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(GiftPageQueryDTO giftPageQueryDTO) {
        PageHelper.startPage(giftPageQueryDTO.getPage(), giftPageQueryDTO.getPageSize());
        Page<GiftVO> page = giftMapper.page(giftPageQueryDTO);

        List<GiftVO> result = page.getResult();

        result.forEach(r -> {
            Category category = categoryMapper.getById(r.getCategoryId());
            r.setCategoryName(category.getName());
        });

        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(result);

        return pageResult;
    }

    /**
     * 礼盒起售、停售
     *
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Gift gift = Gift.builder()
                .id(id)
                .status(status)
                .build();

        giftMapper.update(gift);
    }

    /**
     * 根据id查询礼盒
     *
     * @param id
     * @return
     */
    @Override
    public GiftVO getById(Long id) {
        Gift gift = giftMapper.getById(id);

        List<GiftGoods> giftGoodsList = giftGoodsMapper.getGiftGoodsByGiftId(id);

        GiftVO giftVO = new GiftVO();
        BeanUtils.copyProperties(gift,giftVO);

        giftVO.setGiftGoods(giftGoodsList);

        return giftVO;
    }

    /**
     * 修改礼盒
     *
     * @param giftDTO
     */
    @Override
    @Transactional
    public void update(GiftDTO giftDTO) {
        Gift gift = new Gift();
        BeanUtils.copyProperties(giftDTO, gift);
        giftMapper.update(gift);

        Long giftId = giftDTO.getId();

        List<GiftGoods> giftGoods = giftDTO.getGiftGoods();

        giftGoods.forEach(gg -> {
            gg.setGiftId(giftId);
        });
        giftGoodsMapper.deleteByGiftId(giftDTO.getId());

        giftGoodsMapper.saveBatch(giftGoods);
    }

    /**
     * 批量删除礼盒
     *
     * @param ids
     */
    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        List<Gift> giftList = giftMapper.getByIds(ids);
        for (Gift gift : giftList) {
            if (gift.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.GIFT_ON_SALE);
            }
        }

        ids.forEach(id -> {
            giftMapper.deleteById(id);
            giftGoodsMapper.deleteByGiftId(id);
        });
    }

    /**
     * 根据分类id查询礼盒
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Gift> getByCategoryId(Long categoryId) {
        List<Gift> giftList = giftMapper.getByCategoryId(categoryId);

        return giftList;
    }

    /**
     * 根据礼盒id查询包含的商品
     *
     * @param id
     * @return
     */
    @Override
    public List<GoodsItemVO> getGoodsItemById(Long id) {
        List<GoodsItemVO> goodsItemVOList = giftMapper.getGoodsItemByGiftId(id);
        return goodsItemVOList;
    }


}
